import Vue from 'vue'
import Vuex from 'vuex'
import { getItem, setItem } from '@/utils/storage'
import { identity } from 'lodash'

Vue.use(Vuex)

const USER_KEY = 'toutiao_user'// 对于这种可以定义个常量(避免一些问题)

export default new Vuex.Store({
  state: {
    user: getItem(USER_KEY) || null, // 当前登录用户的登录状态（token等数据）
    inputDate: null ,// 搜索输入框的值
    keepAliveDate:[]//缓存的模块
  },
  mutations: {
    // 修改state中的数据，需要通过mutations来修改
    // （state是store中的state,data是我们自己传的要修改的值）
    setUser (state, data) {
      state.user = data
      // 防止页面刷新，导致store内的数据丢失，使用localStorage，数据持久化
      setItem(USER_KEY, state.user)
    },

    setInputDate (state, data) {
      state.inputDate = data
    },

   addKeepAlive(state,data){
      if(!state.keepAliveDate.includes(data)){
        state.keepAliveDate.push(data)
      }
   },
   
   deleteKeepAlive(state,data){
    let index = state.keepAliveDate.indexOf(data)
      if(index !== -1){
        state.keepAliveDate.splice(index,1)
      }
   }
  

  },
  actions: {
  },
  modules: {
  }
})
